﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using lighttool.NavMesh;

namespace navmesh_ObjToJson
{
    class navHandle
    {
        private void button1_Click(object sender, EventArgs e)
        {

        }
        double xAdd = 0;
        double yAdd = 0;
        double xScale = 1;
        double yScale = 1;
        int selectPoly = -1;
        navVec3 selectVec;
        int selectPoly2 = -1;
        navVec3 selectVec2;
        public PictureBox pictureBox1;
        public bool isloaded =false;

        int[] runway = null;
        navVec3[] waypoints = null;
        public void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (!isloaded) return;
            Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
            g.Clear(Color.Black);
            Pen p = new Pen(Color.Blue, 2);//定义了一个蓝色,宽度为的画笔
            xAdd = (double)this.info.min.x;
            yAdd = (double)this.info.min.z;
            xScale = (this.info.max.x - this.info.min.x) / this.pictureBox1.Width;
            yScale = (this.info.max.z - this.info.min.z) / this.pictureBox1.Height;
            if (info != null)
            {

                for (var polyindex = 0; polyindex < info.nodes.Length; polyindex++)
                {
                    var poly = info.nodes[polyindex].poly;

                    List<Point> polys = new List<Point>();
                    foreach (int po in poly)
                    {
                        var v = this.info.vecs[po];
                        polys.Add(new Point((int)((v.x - xAdd) / xScale), (int)((v.z - yAdd) / yScale)));
                    }
                    if (selectPoly == polyindex)
                    {
                        g.FillPolygon(new SolidBrush(Color.Yellow), polys.ToArray());
                        g.DrawPolygon(new Pen(Color.Blue, 1), polys.ToArray());
                    }
                    else if (selectPoly2 == polyindex)
                    {
                        g.FillPolygon(new SolidBrush(Color.Green), polys.ToArray());
                        g.DrawPolygon(new Pen(Color.Blue, 1), polys.ToArray());
                    }
                    else if (runway != null && runway.Contains(polyindex))
                    {
                        g.FillPolygon(new SolidBrush(Color.Gray), polys.ToArray());
                        g.DrawPolygon(new Pen(Color.White, 1), polys.ToArray());

                    }
                    else
                    {
                        g.FillPolygon(new SolidBrush(Color.Blue), polys.ToArray());
                        g.DrawPolygon(new Pen(Color.White, 1), polys.ToArray());

                    }
                }
            }
            if (waypoints != null && waypoints.Length > 1)
            {
                List<Point> polys = new List<Point>();
                foreach (var v in waypoints)
                {
                    //var v = this.info.vecs[po];
                    polys.Add(new Point((int)((v.x - xAdd) / xScale), (int)((v.z - yAdd) / yScale)));

                }
                g.DrawLines(new Pen(Color.Red, 2), polys.ToArray());
            }
        }

        navMeshInfo info;
        public void Form1_Load(List<float> vs, List<List<int>> fs)
        {
            this.info = Load(vs,fs);
            isloaded = true;
            pictureBox1.Refresh();
        }

        // 加载 构建navmeshinfo
        public static navMeshInfo Load(List<float> vs,List<List<int>> fs)
        {
            //收集 Obj 数据 （三角形模式)

            navMeshInfo info = new navMeshInfo();
            List<navVec3> listVec = new List<navVec3>();

            for (int i = 0; i < vs.Count/3; i++)
            {
                navVec3 v3 = new navVec3();

                v3.x = vs[i*3] * -1; // -x
                v3.y = vs[i*3 +1];
                v3.z = vs[i*3 +2];
                listVec.Add(v3);
            }
            info.vecs = listVec.ToArray();

            List<navNode> polys = new List<navNode>();

            for (int i = 0; i < fs.Count; i++)
            {
                navNode node = new navNode();
                node.nodeID = i;
                List<int> poly = new List<int>();
                List<int> tlist = fs[i];
                for (int j = tlist.Count - 1; j >= 0; j--)
                {
                    poly.Add(tlist[j] -1); //逆向  减1
                }
                node.poly = poly.ToArray();
                node.genBorder();//这里生成的border 是顶点border
                node.genCenter(info);
                polys.Add(node);
            }
           
            info.nodes = polys.ToArray();

            info.calcBound();
            info.genBorder();//这里会修改为 polyborder，这是个偷懒的修改
            return info;
        }

        //json 加载
        public static navMeshInfo LoadMeshInfo(string s)
        {
            var j = MyJson.Parse(s);
            navMeshInfo info = new navMeshInfo();
            List<navVec3> listVec = new List<navVec3>();

            foreach (var json in j.asDict()["v"].AsList())
            {
                navVec3 v3 = new navVec3();

                v3.x = json.AsList()[0].AsDouble();
                v3.y = json.AsList()[1].AsDouble();
                v3.z = json.AsList()[2].AsDouble();
                listVec.Add(v3);
            }
            info.vecs = listVec.ToArray();

            List<navNode> polys = new List<navNode>();
            var list = j.asDict()["p"].AsList();
            for (var i = 0; i < list.Count; i++)

            //foreach (var json in j.asDict()["p"].AsList())
            {
                var json = list[i].AsList();
                navNode node = new navNode();
                node.nodeID = i;
                List<int> poly = new List<int>();
                foreach (var tt in json)
                {
                    poly.Add(tt.AsInt());
                }
                node.poly = poly.ToArray();
                node.genBorder();//这里生成的border 是顶点border
                node.genCenter(info);
                polys.Add(node);
            }
            info.nodes = polys.ToArray();

            info.calcBound();
            info.genBorder();//这里会修改为 polyborder，这是个偷懒的修改
            return info;
        }

        public void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!isloaded) return;
            double x = e.X * xScale + xAdd;
            double y = e.Y * yScale + yAdd;
            var vec = new navVec3();
            vec.x = x;
            vec.y = 0;
            vec.z = y;

            if (e.Button == MouseButtons.Left)
                this.selectPoly = -1;
            else
                this.selectPoly2 = -1;

            for (int p = 0; p < this.info.nodes.Length; p++)
            {
                if (this.info.inPoly(vec, this.info.nodes[p].poly))
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        this.selectPoly = p;
                        this.selectVec = vec.clone();
                    }
                    else
                    {
                        this.selectPoly2 = p;
                        this.selectVec2 = vec.clone();
                    }
                    break;
                }
            }


            if (this.selectPoly >= 0 && this.selectPoly2 >= 0)
            {
                this.runway = pathFinding.calcAStarPolyPath(this.info, this.selectPoly, this.selectPoly2);
                if (this.runway == null)
                {
                    this.waypoints = null;
                    //this.Text = "no link";

                }
                else
                {
                    this.waypoints = pathFinding.calcWayPoints(this.info, this.selectVec, this.selectVec2, this.runway);
                    //this.Text = "link:" + this.runway;
                }
            }
            else
            {
                this.runway = null;
                //this.Text = "no link";
            }

            this.pictureBox1.Refresh();

        }

    }
}
